﻿jQuery.noConflict();

// partial postback
pageLoad = function() {
    if (this.isPostBack != true) {
        this.isPostBack = true;
        
        // Count on initial load
        FreecartService("CountCartItems", null);
    }
    else {
        // displays loads the last panel before updatepanel redraw
        if (jQuery(".menu_active").length > 0) {
            jQuery(".BodyPanel").css("display", "none");
            jQuery("#" + jQuery(".menu_active").attr("expand")).css("display", "block");
        }
        
        // Recount on partial load
        FreecartService("CountCartItems", null);
    }
};

// no postback
jQuery(document).ready(function() {
    jQuery(".menu_item").click(function() {
        jQuery(".menu_active").removeClass("menu_active");
        jQuery(".BodyPanel").css("display", "none");
        jQuery("#" + jQuery(this).attr("expand")).css("display", "block");
        jQuery(this).addClass("menu_active");
    });
});

function FreecartService(method, param, obj) {
    // Available Web Service Calls: UpdateQuantity, DeleteItem, AddItem, CountCartItems
    jQuery.ajax({
        type: "POST",
        url: "./WebService.asmx/" + method,
        data: param,
        async: false,
        contentType: "application/x-www-form-urlencoded",
        dataType: "xml",
        beforeSend: function() {
            if (jQuery("#loading").length == 0 && obj != null) {
                jQuery(obj).before("<img src=\"./Themes/" + jQuery("#hiddenThemeName").val() + "/Images/ajax-loader.gif\" alt=\"Loading...\" id=\"loading\" />");
            }
        },
        success: function(result) {
            if (method == "CountCartItems") {
                jQuery("span#CartCount").text(jQuery(result).find('int').text());
            }
        },
        complete: function() {
            if (jQuery("#loading").length >= 1 && obj != null) {
                jQuery("#loading").remove();
            }
        }
    });
}

function RedrawPanel(UpdatePanelId) {
    __doPostBack(UpdatePanelId, "null");
}